#include "node.hpp"

template<class T>
class BinaryTree {
public:
    BinaryTree() {
        this->root = nullptr;
    }
    void set_root(Node<T> *root) {
        this->root = root;
    }
    // 前序遍历
    void pre_order_traverse() {
        if (root == nullptr)
            throw "An empty binary tree cannot be traversed";
        root->pre_order_traverse();
    }
    // 中序遍历
    void infix_order_traverse() {
        if (root == nullptr)
            throw "An empty binary tree cannot be traversed";
        root->infix_order_traverse();
    }
    // 后序遍历
    void post_order_traverse() {
        if (root == nullptr)
            throw "An empty binary tree cannot be traversed";
        root->post_order_traverse();
    }
    // 前序查找
    Node<T> *pre_order_search(T data) {
        return root->pre_order_search(data);
    }
    // 中序查找
    Node<T> *infix_order_search(T data) {
        return root->infix_order_search(data);
    }
    // 后序查找
    Node<T> *post_order_search(T data) {
        return root->post_order_search(data);
    }
    // 删除结点
    void del_node(T data) {
        if (root != nullptr) {
            if (root->data == data) {
                delete root;
                root = nullptr;
            }
        }else {
            throw "An empty tree cannot be deleted";
        }
    }
private:
    Node<T> *root;
};